package com.study.array;

/**
 * @author ytj
 * @date 2022/1/4
 */
public class Method16 {
    public static void main(String[] args) {
        int[] nums={1};
        System.out.println(searchInsert(nums, 0));
    }

    public static int searchInsert(int[] nums, int target) {
        int left=0,right=nums.length-1;
        int result=0;
        while (left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]<target){
                if(mid+1>=nums.length){
                    result=mid+1;
                }
                if(nums.length>mid+1&&nums[mid+1]>target){
                    result=mid+1;
                }
                left=mid+1;
            }else if(nums[mid]>target){
                right=mid-1;
            }
        }
        return result;
    }
}
